/**********************************************************************************************************************
 *
 * Project:    mGameEngine
 * File:       view.cpp
 * Author:     k.swiniarsk2
 * Created on: Sep 2, 2013
 *
 **********************************************************************************************************************/

#include "view.h"

namespace mGameEngine
{
namespace GUI
{

View::View() : _parent(NULL), _views(), _size(), _position(), _anchor()
{
	// TODO Auto-generated constructor stub
}

View::~View()
{
    for(List<View *>::Iterator i = _views.begin(); i != _views.end(); ++i)
    {
        delete *i;
    }
	// TODO Auto-generated destructor stub
}

View *View::getRootView() const
{
    View *v;
    for(v = const_cast<View *>(this); v->_parent != NULL; v = v->_parent);
    return v;
}

void View::addView(View* view, uint8 position)
{
    // no null views
    if(!view)
    {
        return;
    }

    // detach from previous parent
    if(view->_parent)
    {
        view->_parent->removeView(view);
    }

    // add to list
    if(position == -1)
    {
        _views.push_back(view);
    }
    else
    {
        List<View *>::Iterator i = _views.begin();
        while(position > 0 && i != _views.end())
        {
            --position;
            ++i;
        }
        _views.insert(i, view);
    }

    // set parent view
    view->_parent = this;
}

void View::removeView(View* view)
{
    // no null views
    if(!view)
    {
        return;
    }

    // remove view
    _views.remove(view);
    view->_parent = NULL;
}

void View::removeAllViews()
{
    for(List<View *>::Iterator i = _views.begin(); i != _views.end(); ++i)
    {
        View *view = *i;
        view->_parent = NULL;
    }
    _views.clear();
}

void View::draw() const
{
	// TODO
}

} // namespace GUI
} // namespace mGameEngine
